package com.whl.sm.controller.assets;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whl.sm.common.result.Result;
import com.whl.sm.common.util.UploadUtils;
import com.whl.sm.controller.BaseController;
import com.whl.sm.dto.SelectDto;
import com.whl.sm.model.assets.ComUnits;
import com.whl.sm.model.system.Community;
import com.whl.sm.service.assets.ComBuildingService;
import com.whl.sm.service.assets.ComUnitsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wuhaolun
 * @since 2021-10-09
 */
@RestController
@RequestMapping("/com-units")
@Slf4j
public class ComUnitsController extends BaseController {
    @Autowired
    private ComUnitsService comUnitsService;

    @Autowired
    private ComBuildingService comBuildingService;

    @Value("${ROOT_PATH}")
    private String rootPath;


    @RequestMapping("/list")
    public Result getComList(HttpServletRequest request){
        QueryWrapper queryWrapper = getLikeParam("unit_code");
        queryWrapper.ne("is_del",1);
        queryWrapper.orderByDesc("storey");
        setHttpRequestQueryWrapper(queryWrapper,"bud_id","unit_status");
        Page<Object> units = comUnitsService.page(getPage(),queryWrapper);
        units.getRecords().stream().forEach(obj->{
            ComUnits comUnits = (ComUnits) obj;
            comUnits.setBudName(comBuildingService.getById(comUnits.getBudId()).getBudName());
        });
        return Result.ok(units);
    }

    @RequestMapping("/info/{id}")
    public Result commInfo(@PathVariable("id") String id){
        ComUnits comUnits = comUnitsService.getById(id);
        return Result.ok(comUnits);
    }

    @RequestMapping("/wxCurrent")
    public Result getWxUnitInfoByCurrent(){
        return comUnitsService.getDetailByCurrent();
    }

    @RequestMapping("/update")
    public Result updateUser(@RequestBody ComUnits comUnits){
        comUnitsService.update(comUnits);
        return Result.ok();
    }

    @RequestMapping("/logDelete")
    public Result logicalDelete(@RequestBody List<String> idList){
        for (int i = 0; i <idList.size() ; i++) {
            comUnitsService.update(new UpdateWrapper<ComUnits>()
                    .eq("id",idList.get(i))
                    .set("is_del",1)
            );
        }
        return Result.ok();
    }

    @RequestMapping("/select")
    public Result getByKeyWord(@RequestParam("assetsCode") String assetsCode){
        List<ComUnits> unitsList = comUnitsService.list(new QueryWrapper<ComUnits>().like("unit_code",assetsCode));
        List<SelectDto> result = unitsList.stream().map(obj->{
            return new SelectDto(obj.getId(),obj.getUnitCode(),null,false);
        }).collect(Collectors.toList());
        return Result.ok(result);
    }


    @RequestMapping("/uploadFile")
    public Result upload(@RequestParam MultipartFile unitThumbnail, @RequestParam("id") String id, HttpServletRequest request){
        String path = "/assets/unitThumbnail";
        try {
            List<Map<String,Object>> fileList = UploadUtils.saveFile(request,path,this.rootPath,"unitThumbnail");
            String resultPath = "";
            if (fileList.size()>0){
                for (Map<String,Object> map : fileList){
                    resultPath = (String) map.get("filePath");
                }
            }
            ComUnits targetUnit = comUnitsService.getById(id);
            targetUnit.setThumbnail(resultPath);
            comUnitsService.update(targetUnit);
        }catch (Exception e){
            log.error("资产管理----->单元管理:上传单元缩略图失败!");
            return Result.fail();
        }
        return Result.ok();
    }



}
